/*
 * Copyright (C) 2019 Peng fei Pan <panpfpanpf@outlook.me>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.decode;

import me.panpf.sketch.ErrorTracker;
import me.panpf.sketch.cache.BitmapPool;
import me.panpf.sketch.datasource.DataSource;
import me.panpf.sketch.request.ErrorCause;
import me.panpf.sketch.request.LoadRequest;
import ohos.media.image.ImageSource;
import me.panpf.sketch.Gif;

import java.io.IOException;

public class GifDecodeHelper extends DecodeHelper {

    @Override
    public boolean match(LoadRequest request, DataSource dataSource,
                         ImageType imageType, ImageSource.DecodingOptions boundOptions) {
        if (imageType == ImageType.GIF && request.getOptions().isDecodeGifImage()) {
//            if (SketchGifFactory.isExistGifLibrary()) {
//                return true;
//            } else {
//                HiLog.error("GifDecodeHelper", "Not found libpl_droidsonroids_gif.so. " +
//                        "Please go to “https://github.com/panpf/sketch” find how to import the sketch-gif library");
//            }
            return true;
        }
        return false;
    }

    @Override
    public DecodeResult decode(LoadRequest request, DataSource dataSource,
                               ImageType imageType, ImageSource.DecodingOptions boundOptions,
                               ImageSource.DecodingOptions decodeOptions, int exifOrientation) throws DecodeException {
        ImageSource source = null;
        try {
            source = ImageSource.create(dataSource.getInputStream(), new ImageSource.SourceOptions());
            ImageAttrs imageAttrs = new ImageAttrs(source.getSourceInfo().encodedFormat, boundOptions.desiredSize.width, boundOptions.desiredSize.height, exifOrientation);
            BitmapPool bitmapPool = request.getConfiguration().getBitmapPool();
            Gif gifDrawable = dataSource.makeGifDrawable(request.getKey(), request.getUri(), imageAttrs, bitmapPool);

            return new GifDecodeResult(imageAttrs, gifDrawable).setBanProcess(true);
        } catch (IOException e) {
            throw new DecodeException(e, ErrorCause.DECODE_FILE_IO_EXCEPTION);
        } catch (NotFoundGifLibraryException e) {
            throw new DecodeException(e, ErrorCause.DECODE_NOT_FOUND_GIF_LIBRARY);
        } catch (UnsatisfiedLinkError | ExceptionInInitializerError e) {
            request.getConfiguration().getErrorTracker().onNotFoundGifSoError(e);
            throw new DecodeException(e, ErrorCause.DECODE_NO_MATCHING_GIF_SO);
        } catch (Throwable e) {
            ErrorTracker errorTracker = request.getConfiguration().getErrorTracker();
            errorTracker.onDecodeGifImageError(e, request, boundOptions.desiredSize.width, boundOptions.desiredSize.height, source.getSourceInfo().encodedFormat);
            throw new DecodeException(e, ErrorCause.DECODE_UNABLE_CREATE_GIF_DRAWABLE);
        }
    }
}
